<template>
  <div class="box">
    <div class="header">
       <van-nav-bar
        title="添加地址"
        left-arrow
        @click-left="$router.back()"
      />
    </div>
    <div class="content">
      <!-- 地址编辑 -->
      <van-address-edit
        :area-list="areaList"
        :address-info="initaddress"
        show-postal
        show-delete
        show-set-default
        save-button-text="保存并且使用该地址"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, AddressEdit, Toast, Area } from 'vant'
import { areaList } from '@vant/area-data'
import { updateOrderAddress } from '../../api/order'
import { addAddress, updateAddress, deleteAddress } from '../../api/address'
Vue.use(AddressEdit)
Vue.use(Toast)
Vue.use(Area)
Vue.use(NavBar)
Vue.use(updateOrderAddress)
export default {
  data () {
    return {
      areaList,
      initaddress: {}
    }
  },
  computed: {
    isUpdateaddress () {
      return Boolean(this.$route.params.item.addressid)
    }
  },
  mounted () {
    if (this.isUpdateaddress) {
      this.initaddress = {
        name: this.$route.params.item.name,
        tel: this.$route.params.item.tel,
        province: this.$route.params.item.province,
        city: this.$route.params.item.city,
        county: this.$route.params.item.county,
        addressDetail: this.$route.params.item.addressDetail,
        isDefault: this.$route.params.item.isDefault
      }
    }
  },
  methods: {
    onSave (content) {
      if (!this.isUpdateaddress) {
        console.log(this.$route.params)
        content.userid = localStorage.getItem('userid')
        addAddress(content).then(res => {
          content.time = this.$route.params.time
          console.log('添加地址成功', content)
          console.log('res', content)
          updateOrderAddress(content).then((res1) => {
            // this.$router.go(-2)
            console.log('res1', res1)
          })
        })
      } else {
        console.log('else' + content)
        updateAddress({
          addressid: this.$route.params.item.addressid,
          userid: localStorage.getItem('userid'),
          name: content.name,
          tel: content.tel,
          province: content.province,
          city: content.city,
          county: content.county,
          addressDetail: content.addressDetail,
          isDefault: content.isDefault
        }).then(res => console.log('res2' + res))
      }
    },
    onDelete () {
      if (this.isUpdateaddress) {
        deleteAddress({
          addressid: this.$route.params.item.addressid
        }).then(res => {
          this.$router.back()
        })
      } else {
        this.$router.back()
      }
    }
  }
}
</script>
